import Vue from 'vue';
import { PopupManager } from '@/utils/popup';
import GMessage from './g-message.vue';

let MessageConstructor = Vue.extend( GMessage );

let instance;
let instances = [];
let seed = 1;

var Message = function ( options ) {
    options = options || {};
    if ( typeof options === 'string' ) {
        options = {
            message: options
        };
    }
    let userOnClose = options.onClose;
    let id = 'message_' + seed++;

    options.onClose = function () {
        Message.close( id, userOnClose );
    };

    instance = new MessageConstructor( {
        data: options
    } );
    instance.id = id;
    instance.vm = instance.$mount();
    document.body.appendChild( instance.vm.$el );
    instance.vm.visible = true;
    instance.dom = instance.vm.$el;
    instance.dom.style.zIndex = PopupManager.nextZIndex();
    instances.push( instance );
    return instance.vm;
};

[ 'success', 'warning', 'info', 'error' ].forEach( type => {
    Message[ type ] = options => {
        if ( typeof options === 'string' ) {
            options = {
                message: options
            };
        }
        options.type = type;
        return Message( options );
    };
} );

Message.close = function ( id, userOnClose ) {
    for ( let i = 0, len = instances.length; i < len; i++ ) {
        if ( id === instances[ i ].id ) {
            if ( typeof userOnClose === 'function' ) {
                userOnClose( instances[ i ] );
            }
            instances.splice( i, 1 );
            break;
        }
    }
};

Message.closeAll = function () {
    for ( let i = instances.length - 1; i >= 0; i-- ) {
        instances[ i ].close();
    }
};

export default Message;
